<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/urlUtils.js"></script>
    <style>
        .nav {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }

        .container {
            padding-top: 80px;
            height: auto;
        }

        .container-right {
            width: auto;
        }

        .blog-pagnation-wrapper {
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }

        .blog-pagnation-item {
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }

        .blog-pagnation-item:hover {
            background: #4e4eeb;
            color: #fff;
        }

        .blog-pagnation-item.actvie {
            background: #4e4eeb;
            color: #fff;
        }

    </style>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo2.jpg" alt="">
    <span class="title"><a href="myblog_list.html">我的博客系统</a></span>
    <!-- 用来占据中间位置 -->
    <span class="spacer"></span>
    <a href="blog_list.html">主页</a>
    <a href="blog_add.html">写博客</a>
    <a href="login.html">登录</a>
    <!-- <a href="#">注销</a> -->
</div>
<!-- 版心 -->
<div class="container">
    <!-- 右侧内容详情 -->
    <div class="container-right" style="width: 100%;">
        <!-- 每一篇博客包含标题, 摘要, 时间 -->
        <div id="blogListDiv">
            <!--            <div class="blog">-->
            <!--                <div class="title">我的第一篇博客</div>-->
            <!--                <div class="date">2021-06-02</div>-->
            <!--                <div class="desc">正文</div>-->
            <!--                <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>-->
            <!--            </div>-->
        </div>
        <hr>
        <div class="blog-pagnation-wrapper">
            <button class="blog-pagnation-item" onclick="doFirst()">首页</button>
            <button class="blog-pagnation-item" onclick="doBefore()">上一页</button>
            <button class="blog-pagnation-item" onclick="doAfter()">下一页</button>
            <button class="blog-pagnation-item" onclick="doLast()">末页</button>
            &nbsp;&nbsp;&nbsp;&nbsp;
            当前在第<span id="page"></span>页
            &nbsp;
            共<span id="page_count"></span>页
        </div>
    </div>
</div>
<script>
    var psize;
    var pindex;
    var totalPage = 1;

    function init() {
        // 1.处理分页参数
        psize = getValueOfUrl('psize');
        if (psize == null) {
            psize = 3;
        }
        pindex = getValueOfUrl('pindex');
        if (pindex == null) {
            pindex = 1;
        }
        jQuery("#page").html(pindex);
        // 2.向后端请求数据
        jQuery.ajax({
            type: 'get',
            url: 'art/getlistbypage',
            data: {
                'psize': psize,
                'pindex': pindex
            },
            success: function (res) {
                // 将结果展示给用户
                if (res.code == 200 && res.data != null) {
                    var createHtml = "";
                    if (res.data.list != null && res.data.list.length > 0) {
                        totalPage = res.data.count;
                        jQuery("#page_count").html(totalPage);
                        var artList = res.data.list;
                        for (var i = 0; i < artList.length; i++) {
                            createHtml += '<div class="blog">';
                            createHtml += '<div class="title">' + artList[i].title + '</div>';
                            createHtml += '<div class="date">' + artList[i].createtime + '</div>';
                            createHtml += '<div class="desc">' + artList[i].content + '</div>';
                            createHtml += '<a href="blog_content.html?id=' + artList[i].id
                                + '" class="detail">查看全文 &gt;&gt;</a>';
                            createHtml += '</div>';
                        }
                    } else {
                        createHtml += "<h3 style='margin-top: 30px; margin-left: 30px'>暂无文章</h3>"
                    }
                    jQuery("#blogListDiv").html(createHtml);
                } else {
                    alert("查询失败!" + res.msg);
                }
            }
        });
    }

    init();

    function doFirst() {
        // 判断是否在首页
        if (pindex == 1) {
            alert("已经在首页了,无需跳转!");
            return;
        }
        // 跳转
        location.href = 'blog_list.html?pindex=1';
    }

    function doLast() {
        // 判断是否在末页
        if (pindex == totalPage) {
            alert("已经在末页了,无需跳转!");
            return;
        }
        // 跳转
        location.href = 'blog_list.html?pindex=' + totalPage;
    }

    function doBefore() {
        // 判断是否在首页
        if (pindex == 1) {
            alert("已经在首页了,无需跳转!");
            return;
        }
        // 跳转
        location.href = 'blog_list.html?pindex=' + (parseInt(pindex) - 1);
    }

    function doAfter() {
        // 判断是否在末页
        if (pindex == totalPage) {
            alert("已经在末页了,无需跳转!");
            return;
        }
        // 跳转
        location.href = 'blog_list.html?pindex=' + (parseInt(pindex) + 1);
    }
</script>
</body>
</html>